﻿using System;


using System.Text;
using IAM.Interface.Model.ENUM;
using IAM.Interface.Model.Interface;
using System.Xml;
using System.Web;
using System.Reflection;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 单条代理设置信息
    /// 每条代理设置信息描述了该人员在一个模块上的代理设置
    /// </summary>
    public class Delegation : AbstractConvertObject,IConvertXml
    {
        #region 属性
        private string _moduleid = "";
        private ModuleType _type = ModuleType.Delegate;
        private DateTime _begindate = System.DateTime.Now;
        private DateTime _enddate = System.DateTime.Now;
        AccountInfo _delegator = null;
        /// <summary>
        /// 设置代理的模块标识
        /// </summary>
        public string Module
        {
            get
            {
                return _moduleid;
            }
            set
            {
                _moduleid = value;
            }
        }
        /// <summary>
        /// 代理类型
        /// </summary>
        public ModuleType Type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
            }

        }
        /// <summary>
        /// 代理生效起始日期
        /// </summary>
        public DateTime BeginDate
        {
            get
            {
                return _begindate;
            }
            set
            {
                _begindate = value;
            }
        }
        /// <summary>
        /// 代理生效结束日期
        /// </summary>
        public DateTime EndDate
        {
            get
            {
                return _enddate;
            }
            set
            {
                _enddate = value;
            }
        }
        /// <summary>
        /// 代理人信息
        /// </summary>
        public AccountInfo Delegator
        {
            get
            {
                return _delegator;
            }
            set
            {
                _delegator = value;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append(" <Delegation");
                _xml.Append(" Module=\"" + IAM.Interface.Common.EnDeCoding.Encode(this.Module )+ "\"");
                _xml.Append(" Type=\"" + this.Type.ToString()+ "\"");
                _xml.Append(" BeginDate=\"" + this.BeginDate.ToString() + "\"");
                _xml.Append(" EndDate=\"" + this.EndDate.ToString() + "\"");
                _xml.Append(" >");
                _xml.Append("<Delegator>");
                _xml.Append(Delegator.XML);
                _xml.Append("</Delegator>");
                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</Delegation>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            this.Module = IAM.Interface.Common.EnDeCoding.Decode(this.CurrentXmlNode.Attributes["Module"].InnerText);
            string strType = this.CurrentXmlNode.Attributes["Type"].InnerText;
            switch (strType.ToLower())
            {
                case "delegate":
                    this.Type = ModuleType.Delegate;
                    break;
                case "operateas":
                    this.Type = ModuleType.OperateAs;
                    break;
                case "both":
                    this.Type = ModuleType.Both;
                    break;
            }
            this.BeginDate = System.Convert.ToDateTime(this.CurrentXmlNode.Attributes["BeginDate"].InnerText);
            this.EndDate = System.Convert.ToDateTime(this.CurrentXmlNode.Attributes["EndDate"].InnerText);
            XmlNode xn = this.CurrentXmlNode.FirstChild;//Delegator
            xn = xn.FirstChild;//AccountInfo
            //AccountInfo
            this.Delegator = new AccountInfo();
            this.Delegator.CurrentXmlNode = xn;
            this.Delegator = (AccountInfo)this.Delegator.ConvertToObject();
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 Delegation 对象是否等效于当前 Delegation
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            Delegation delegation = null;
            try
            {
                delegation = (Delegation)obj;
            }
            catch
            {
                return false;
            }
            bool result = true;

            if (this.Module != delegation.Module)
            {
                result = false;
            }
            else if (this.Type != delegation.Type)
            {
                result = false;
            }
            else if (this.Delegator.Equals(delegation.Delegator))
            {
                result = false;
            }
            return result;
        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
    }
}
